package com.acme.bankapp.service.bank;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.bank.Client;
import com.acme.bankapp.domain.client.ClientFactory;
import com.acme.bankapp.exceptions.ClientExistsException;
import com.acme.bankapp.util.FeedFileLineParser;
import com.acme.bankapp.util.ParseException;

public class BankDataLoaderService {
    /**
     * Loads a file which contains data feed in the following format: <br>
     * accountid=1;accounttype=c|s;balance=100;overdraft=50;name=John;gender=m|f
     * ;
     * 
     * @param bank
     * @param fileName
     * @throws ParseException
     * @throws ClientExistsException
     * @throws IOException
     */
    public static void load(final Bank bank, final String fileName)
	    throws ParseException, ClientExistsException, IOException {

	BufferedReader bufferedReader = new BufferedReader(new FileReader(
		fileName));
	String line;
	try {
	    while ((line = bufferedReader.readLine()) != null) {
		FeedFileLineParser feedFileLineParser = new FeedFileLineParser(
			line);
		Client client = ClientFactory.createClient(feedFileLineParser);
		bank.addClient(client);
	    }
	} catch (IOException e) {
	    throw e;
	} finally {
	    bufferedReader.close();
	}
    }

    public BankService getBankService() {
	return bankService;
    }

    public void setBankService(final BankService bankService) {
	this.bankService = bankService;
    }

    private BankService bankService;
}
